<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Model\Message;
use App\Model\Order;
use Session;

class IndexController extends Controller
{
    // 后台首页
    public function index()
    {
        $number = $this->getIndexNum();
        $comments = $this->getNewComments();
        $orders = new Order();
        $ordersList = $orders->getOrderList();
        return view('admin.index', compact('number', 'comments', 'ordersList'));
    }
    
    // 列表页面
    public function lists($tmp = 1)
    {
        return view('admin.list' . $tmp);
    }
    
    // 编辑、添加页面
    public function edit()
    {
        return view('admin.edit');
    }
    
    // 获取首页数量信息
    protected function getIndexNum()
    {
        $number = [];
        $time = date('Y-m-d');
        // 新订单
        $number['order'] = DB::table('order')->where('order_paytime', '>=', $time)->count();
        // 新注册会员
        $number['user'] = DB::table('user')->where('user_regtime', '>=', $time)->count();
        // 最新评论数
        $number['comment'] = DB::table('comment')->where('comment_time', '>=', $time)->count();
        return $number;
    }
    
    // 获取最新评论
    public function getNewComments()
    {
        // 先获取评论信息
        $comments = DB::table('comment')->where('comment_status', 1)
                ->orderBy('id', 'desc')
                ->limit(5)
                ->get();
        // 获取用户信息
        foreach($comments as $c){
            $c->user = DB::table('user')->where('id', $c->user_id)->select(['user_nickname', 'user_avartar'])->first();
        }
        
        return $comments;
    }
}
